package com.spzx.product.service.impl;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.ProductSpecService;
import com.spzx.product.vo.ProductSpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductSpecServiceImpl implements ProductSpecService {

    @Autowired
    private ProductSpecMapper productSpecMapper;

    @Override
    public List<ProductSpecVo> pageList(ProductSpec productSpec) {
        return productSpecMapper.selectList(productSpec);
    }

    @Override
    public ProductSpecVo getInfo(Long id) {
        ProductSpecVo vo = productSpecMapper.getInfo(id);
        List<Long> categoryIdList = new ArrayList<>();
        categoryIdList.add(vo.getCategory1Id());
        categoryIdList.add(vo.getCategory2Id());
        categoryIdList.add(vo.getCategoryId());
        vo.setCategoryIdList(categoryIdList);
        return vo;
    }

    @Override
    public void save(ProductSpec productSpec) {
        try {
            productSpecMapper.save(productSpec);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("该商品规格已添加过");
        }
    }

    @Override
    public void update(ProductSpec productSpec) {
        try {
            productSpecMapper.update(productSpec);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("该商品规格已添加过");
        }
    }

    @Override
    public void delete(Long[] ids) {
        productSpecMapper.delete(ids);
    }

    @Override
    public List<ProductSpec> selectProductSpecListByCategoryId(Long categoryId) {
        List<ProductSpec> list = productSpecMapper.selectProductSpecListByCategoryId(categoryId);
        return list;
    }


}
